import { Component, OnInit, ViewChild } from '@angular/core';

import { CudTableService } from '../../public/serve/cud-table/cud-table.service';
import { RemoveTableService } from 'app/public/serve/remove-table/remove-table.service';
import { InitService } from 'app/public/serve/init/init.service';
import { Service } from '../../server/server';

import { ScopesTableComponent } from './scopes-table/scopes-table.component';

import { scope } from './scope.data';
import { scopeUrl } from './scope.url';

declare var $:any;

@Component({
  selector: 'app-scope-authorization',
  templateUrl: './scope-authorization.component.html',
})
export class ScopeAuthorizationComponent implements OnInit {

  @ViewChild('table') table : ScopesTableComponent;

  right: any = { "system" : {}, "scope-authorization" : {} };
  constructor(
    public removeTableService: RemoveTableService,
    public initService: InitService,
    private request: CudTableService, 
    private server:Service
  ) { 
    this.right = $.extend(this.right, server.getRight());
  }
  
  isActive= this.server.isActive;
  ngOnInit() {
    this._serviceType();
    this.server.eventEmit.subscribe((value: any) => { this.isActive=value; });
  }

  ngDoCheck(){
    this.right = $.extend(this.right, this.server.getRight());
  }

  // 表格
  totalItems : number;
  totalItemsFromTableHandler(totalItems: number) {
    this.totalItems = totalItems;
  }

  /**
   * 分页
   * @param pagination 
   */ 
  _pagination(pagination: Array<any>) {

    scope.pagination.id         = pagination[0].id;
    scope.pagination.pageNumber = pagination[0].pageNumber;
    scope.pagination.pageSize   = pagination[0].pageSize;
    scope.pagination.naturalNum = (scope.pagination.pageNumber - 1) * scope.pagination.pageSize;

    this.table._table();
  }

  /**
   * 检索
   * @param val
   */ 
  _retrieval(val: string) {
    scope.queryCriteria = val.trim();
    this.table._table();
  } 

  /**
   * 服务类型
   */
  private _serviceType() {
    this.request
        .getRequest(this.server.baseUrl +scopeUrl.serviceType)
        .then(res => {
          scope.serviceType = res.result;
        })
        .catch(err => {
          return
        });
  }

}